package com.vhuomai.wechat.handler;

import com.quanyue.auto.cache.core.CacheService;
import com.quanyue.auto.util.AssertUtils;
import com.vhuomai.wechat.constant.WxConstant;
import com.vhuomai.wechat.service.WxAccountService;
import com.vhuomai.weixin.mp.api.WxMpInMemoryConfigStorage;
import org.springframework.beans.factory.annotation.Autowired;

public class MultiWxMpConfigStorage extends WxMpInMemoryConfigStorage {
    
    @Autowired
    private WxAccountService accountService;
    @Autowired
    private CacheService cacheService;
    
    private static final String ACCESS_KEY = "WX_ACCESS_KEY";
    private static final String JSAPI_TICKET_KEY = "JSAPI_TICKET_KEY";
    private static final String CARDAPI_TICKET_KEY = "CARDAPI_TICKET_KEY";
    
    @Override
    public String getAppId() {
        String appId = accountService.findOne().getAppId();
        AssertUtils.notBlank(appId, "请先配置公众号");
        return appId;
    }
    
    @Override
    public String getSecret() {
        return accountService.findOne().getSecret();
    }
    
    @Override
    public String getToken() {
        return accountService.findOne().getToken();
    }
    
    @Override
    public String getAesKey() {
        return accountService.findOne().getAesKey();
    }
    
    @Override
    public String getAccessToken() {
        return cacheService.get(WxConstant.WX_TOKEN_CACHE, ACCESS_KEY.concat(getAppId()), String.class);
    }
    
    @Override
    public boolean isAccessTokenExpired() {
        return getAccessToken() == null;
    }
    
    @Override
    public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
        cacheService.set(WxConstant.WX_TOKEN_CACHE, ACCESS_KEY.concat(getAppId()), accessToken);
    }
    
    @Override
    public void expireAccessToken() {
        cacheService.remove(WxConstant.WX_TOKEN_CACHE, ACCESS_KEY.concat(getAppId()));
    }
    
    @Override
    public String getJsapiTicket() {
        return cacheService.get(WxConstant.WX_TOKEN_CACHE, JSAPI_TICKET_KEY.concat(getAppId()), String.class);
    }
    
    @Override
    public boolean isJsapiTicketExpired() {
        return getJsapiTicket() == null;
    }
    
    @Override
    public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
        cacheService.set(WxConstant.WX_TOKEN_CACHE, JSAPI_TICKET_KEY.concat(getAppId()), jsapiTicket);
    }
    
    @Override
    public void expireJsapiTicket() {
        cacheService.remove(WxConstant.WX_TOKEN_CACHE, JSAPI_TICKET_KEY.concat(getAppId()));
    }
    
    @Override
    public String getCardApiTicket() {
        return cacheService.get(WxConstant.WX_TOKEN_CACHE, CARDAPI_TICKET_KEY.concat(getAppId()), String.class);
    }
    
    @Override
    public boolean isCardApiTicketExpired() {
        return getCardApiTicket() == null;
    }
    
    @Override
    public synchronized void updateCardApiTicket(String cardApiTicket, int expiresInSeconds) {
        cacheService.set(WxConstant.WX_TOKEN_CACHE, CARDAPI_TICKET_KEY.concat(getAppId()), cardApiTicket);
    }
    
    @Override
    public void expireCardApiTicket() {
        cacheService.remove(WxConstant.WX_TOKEN_CACHE, CARDAPI_TICKET_KEY.concat(getAppId()));
    }
}
